package edu.njfu.myapplication1

import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import edu.njfu.homeork.adapter.StudentAdapterByRecyclerView
import edu.njfu.homeork.model.Student
import edu.njfu.homeork.service.StudentService
import edu.njfu.homeork.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_students_info.*
import kotlinx.android.synthetic.main.student_info_item.*
import retrofit2.Call
import retrofit2.Response

class SearchActivity : AppCompatActivity() {

    var bitmap: Bitmap? =null
//    lateinit var students:List<Student>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_students_info)
        var resultByName=intent.getStringExtra("stduentName")
        if (resultByName != null) {
            val serviceCreator=ServerCreator.create<StudentService>()
            serviceCreator.GetStudentByName(resultByName).enqueue(object :retrofit2.Callback<List<Student>> {
                override fun onFailure(call: Call<List<Student>>, t: Throwable) {
                    TODO("Not yet implemented")
                    Toast.makeText(this@SearchActivity,"查询失败",Toast.LENGTH_SHORT).show()
                }
                override fun onResponse(
                    call: Call<List<Student>>,
                    response: Response<List<Student>>
                ) {
                    Log.d("null", "type==1")

                    var students = response.body()

                    if (students!= null) {
                        Log.d("null", "resuode   aaaaa  :isg is nul;l")
                        val layoutManager = LinearLayoutManager(this@SearchActivity)
                        recyclerView.layoutManager = layoutManager
                        val adapter =  StudentAdapterByRecyclerView(students as ArrayList<Student>)
                        recyclerView.adapter = adapter
                    } else {
                        Toast.makeText(this@SearchActivity, "查询无结果", Toast.LENGTH_LONG).show()
                    }
                }
            })
        }

        searchBtn.setOnClickListener{
            var name=searchName.text.toString();
            var searchIntent= Intent(this,SearchActivity::class.java)
            searchIntent.putExtra("stduentName",name)
            searchIntent.putExtra("type","1")
            startActivity(searchIntent)
        }
    }
}